package com.glory.hikvisionmobile.adapter;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.glory.hikvisionmobile.R;
import com.glory.hikvisionmobile.bean.from.FromStepBean;

import java.util.List;

/**
 * FileName: IQPCListAdapter
 * Author: XJH
 * Date: 2020/3/16 11:14
 * Description:
 * History:
 */
public class IQPCListAdapter extends BaseQuickAdapter<FromStepBean, BaseViewHolder>{
    private boolean enable = true;

    public IQPCListAdapter(@Nullable List<FromStepBean> data) {
        super(R.layout.adapter_param_list,data);
    }

    @Override
    protected void convert(BaseViewHolder helper, FromStepBean item) {
        helper.setText(R.id.tvItem1,item.getInspectionItems());
        helper.setText(R.id.tvItem2,item.getTestMethod());
        helper.setText(R.id.tvItem3,item.getSamplingRatio());
        helper.setGone(R.id.tvItem4,false);
        helper.setGone(R.id.tvItem5,false);
        EditText etResult = helper.getView(R.id.etResult);
        etResult.setVisibility(View.VISIBLE);
        etResult.setEnabled(enable);
        helper.setText(R.id.etResult,item.getTestResult());
        if (etResult.getTag() instanceof TextWatcher) {
            etResult.removeTextChangedListener((TextWatcher) etResult.getTag());
        }
        setEditListener(etResult,item);


    }

    public void setEnable(boolean enable){
        this.enable = enable;
    }


    private void setEditListener(EditText editText, final FromStepBean item) {
        if (editText.getTag() instanceof TextWatcher) {
            editText.removeTextChangedListener((TextWatcher) editText.getTag());
        }
        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                item.setTestResult(s.toString());
            }
        };
        editText.addTextChangedListener(watcher);
        editText.setTag(watcher);
    }
}
